package core{
	import flash.events.EventDispatcher;
	[Event(name = "changed", type = "com_zszen.base.Ratio")]
	public class ScaleRatio extends EventDispatcher{
		//static
		public static const Scale_Default:String = "default"
		public static const Scale_OnlyBigger:String = "bigger"
		public static const Scale_OnlySmaller:String = "smaller"
		public static const Scale_Fit:String = "fit"
		public static const Scale_FitWidth:String = "fitwidth"
		public static const Scale_FitHeight:String = "fitheight"
		public static function scaleTo(target:Object, toWidth:Number, toHeight:Number , scaleType:String="default"):Array {
			if (target["width"] == null||target["height"]==null) {
				throw "scaled target don't hava width/height param"
			}
			var w:Number = target.width
			var h:Number = target.height
			var ps:Number 
			var pso:Number 
			switch(scaleType) {
			case Scale_Default:
				ps = w / h
				pso = toWidth / toHeight
				if (ps > pso) {
					w = toWidth
					h = toWidth/ps
				}else {
					h = toHeight
					w = toHeight*ps
				}
				break
			case Scale_OnlyBigger:
				ps = w / h
				pso = toWidth / toHeight
				if (w > toWidth || h>toHeight) {
					if (ps > pso) {
						w = toWidth
						h = toWidth/ps
					}else {
						h = toHeight
						w = toHeight*ps
					}
				}
				break
			case Scale_OnlySmaller:
				ps = w / h
				pso = toWidth / toHeight
				if (w < toWidth || h < toHeight) {
					if (ps > pso) {
						w = toWidth
						h = toWidth/ps
					}else {
						h = toHeight
						w = toHeight*ps
					}
				}
				break
			case Scale_Fit:
				w = toWidth
				h = toHeight
				break
			case Scale_FitWidth:
				w = toWidth
				break
			case Scale_FitHeight:
				h = toHeight 
				break
			default:
			}
			target.width = w
			target.height = h
			return [w,h]
		}
		public static function scaleInfoTo(nowWidth:Number,nowHeight:Number, toWidth:Number, toHeight:Number , scaleType:String="default"):Array {
			var w:Number = nowWidth
			var h:Number = nowHeight
			var ps:Number 
			var pso:Number 
			switch(scaleType) {
			case Scale_Default:
				ps = w / h
				pso = toWidth / toHeight
				if (ps > pso) {
					w = toWidth
					h = toWidth/ps
				}else {
					h = toHeight
					w = toHeight*ps
				}
				break
			case Scale_OnlyBigger:
				ps = w / h
				pso = toWidth / toHeight
				if (w > toWidth || h>toHeight) {
					if (ps > pso) {
						w = toWidth
						h = toWidth/ps
					}else {
						h = toHeight
						w = toHeight*ps
					}
				}
				break
			case Scale_OnlySmaller:
				ps = w / h
				pso = toWidth / toHeight
				if (w < toWidth || h < toHeight) {
					if (ps > pso) {
						w = toWidth
						h = toWidth/ps
					}else {
						h = toHeight
						w = toHeight*ps
					}
				}
				break
			case Scale_Fit:
				w = toWidth
				h = toHeight
				break
			case Scale_FitWidth:
				w = toWidth
				break
			case Scale_FitHeight:
				h = toHeight
				break
			default:
			}
			return [w,h]
		}
	}
}